<?php
namespace Home\Controller;
class IndexController extends CommonController {
    public function index()
    {
        $this->display();
    }
    /*
     * 列出文章*/
    public function Article()
    {
        $page= new \Think\Page(M('Article')->where(array('status'=>1))->count(),2);
        $this->articles=M('article')->where(array('status'=>1))
                                    ->order('sort desc')
                                    ->field('aid,title,description,add_time,content,click,thumb')
                                    ->limit($page->firstRow.','.$page->listRows)
                                    ->select();

        $page->setConfig('next','下一页');
        $page->setConfig('prev',' 上一页');
        $this->page=$page->show();
//        p($this->articles);die();
        $this->display();
    }
    /*
     * 列出类*/
    public  function category($id){
        $page= new \Think\Page(M('Article')->where(array('status'=>1,'category_cid'=>$id))->count(),2);
        $this->articles=M('article')->where(array('status'=>1,'category_cid'=>$id))
            ->order('sort desc')
            ->field('aid,title,description,add_time,content,click,thumb')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $page->setConfig('next','下一页');
        $page->setConfig('prev',' 上一页');
        $this->page=$page->show();
        $this->display();
    }

    /*
     * 列出标签*/
    public  function  tags($tid){
        $page= new \Think\Page(M('Article_tag')->where(array('tid'=>$tid))->count(),2);
        $this->articles=M('article_tag')->where(array('status'=>1,'tid'=>$tid))
            ->join("blog_article ON blog_article.aid=blog_article_tag.aid")
            ->order('sort desc')
            ->field('blog_article.aid,title,description,add_time,content,click,thumb')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $page->setConfig('next','下一页');
        $page->setConfig('prev',' 上一页');
        $this->page=$page->show();
        $this->display();
    }

    /*
     * 列出内容*/
    public function  view($aid){
       $article=M('article')->where(array('aid'=>$aid))->find();
       M('article')->where(array('aid'=>$aid))->setInc('click',1);
        $article['content']=html_entity_decode($article['content']);
        $this->article=$article;
//        查询文章的标签
        $this->tags=M('article_tag')
            ->join("blog_article ON blog_article.aid=blog_article_tag.aid")
            ->join("blog_tag ON blog_tag.tid=blog_article_tag.tid")
            ->where(array('blog_article.aid'=>$aid))
            ->select();
//        查询文章的有关文章
        $category_id=M('article')->where(array('aid'=>$aid))->getField('category_cid');
        $this->hot_article=M('article')->where(array('category_cid'=>$category_id))->limit(4)->select();
        //p($this->hot_article);die();
        //p($category_id);die();
         //p($this->tags);die();
        $this->display();
    }


}